package com.vancouver.knows.ui.fragment.my;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.R;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.task.mark.DeleteUserBusinessImagesTaskMark;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.util.AppUtils;

import java.util.Set;
import java.util.TreeSet;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Chris Li on 2016-08-23.
 */
public class UserImageGalleryActivity extends BaseActivity implements IResultReceiver, UserImageGalleryRecyclerView.ImageGalleryListener {

    @Bind(R.id.toolbar_title)
    TextView mToolbarTitle;
    @Bind(R.id.menu_item)
    TextView mMenuItem;
    @Bind(R.id.root_view)
    RelativeLayout mRootView;
    @Bind(R.id.loading_view)
    RelativeLayout mLoadingView;

    private BusinessModule bModule;
    private UserImageGalleryPullRefreshView mController;
    private Set<String> mSelectedImages = new TreeSet<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_image_gallery);

        mMenuItem.setText(R.string.menu_del);
        mMenuItem.setEnabled(false);

        mController = new UserImageGalleryPullRefreshView(this);
        mController.loadData();
        RelativeLayout.LayoutParams controllerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        controllerLayoutParams.addRule(RelativeLayout.BELOW, R.id.toolbar);
        mController.setLayoutParams(controllerLayoutParams);
        mRootView.addView(mController);

        bModule = imContext.getBusinessModule();

        mToolbarTitle.setText(getString(R.string.user_image_gallery, bModule.getCacheManager().getUserBusinessImages().size()));
    }

    @Override
    public void onImageSelected(Set<String> selectedImages) {
        if (selectedImages.size() > 0) {
            mMenuItem.setEnabled(true);
            mMenuItem.setTextColor(ContextCompat.getColor(this, R.color.orange));
        } else {
            mMenuItem.setEnabled(false);
            mMenuItem.setTextColor(ContextCompat.getColor(this, R.color.common_light_gray));
        }
        mSelectedImages = selectedImages;
    }

    @Override
    public void onImagesLoaded(int total) {
        mToolbarTitle.setText(getString(R.string.user_image_gallery, total));
    }

    @OnClick(R.id.menu_item)
    public void onDeleteButtonClicked() {
        showDeleteDialog();
    }

    @OnClick(R.id.back_icon)
    public void onBackIconClicked() {
        onBackPressed();
    }

    private void showDeleteDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme);
        builder.setTitle(R.string.delete_prompt);
        builder.setPositiveButton(R.string.menu_del, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                deleteUserImages();
            }
        });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private void deleteUserImages() {
        mLoadingView.setVisibility(View.VISIBLE);
        DeleteUserBusinessImagesTaskMark deleteUserBusinessImagesTaskMark = bModule.getTaskMarkPool().getDeleteUserBusinessImagesTaskMark();
        String userId = AppUtils.getUserId(bModule);
        bModule.getServiceWrapper().deleteUserBusinessImages(this, deleteUserBusinessImagesTaskMark, userId, mSelectedImages);
    }

    @Override
    public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
        if (exception == null) {
            mController.onRefresh(mController);
        }

        mLoadingView.setVisibility(View.GONE);
    }
}
